package com.jeomo.mem.provider.web;

import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.common.log.annotation.LogRecord;
import com.jeomo.mem.provider.request.MemberPointsActivityReq;
import com.jeomo.mem.starter.entity.MemberPointsActivity;
import com.jeomo.mem.starter.service.IMemPointsActivityInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 会员积分活动基础信息
 * @author:Jackpot
 * @create 2020-09-30 12:24
 * @description:
 */
@RestController
@RequestMapping("/member/act")
public class MemPointsActivityController {

    @Autowired
    private IMemPointsActivityInfoService memPointsActInfoService;

    @ApiOperation("获取积分活动基础信息")
    @GetMapping("/base")
    public CommonResult getPointsActivityInfo(){

        List<MemberPointsActivity> allPointsActivities = new ArrayList<>();

        Map<Object, Object> objectObjectMap = memPointsActInfoService.queryCache();
        Set<Object> codes = objectObjectMap.keySet();
        for (Object code : codes) {
            allPointsActivities.add(new MemberPointsActivity(code.toString(),objectObjectMap.get(code).toString()));
        }
        return CommonResult.success(allPointsActivities);
    }

    @LogRecord(module = "积分活动",type = "新增积分活动")
    @ApiOperation("新增积分活动基础信息")
    @PostMapping("/base")
    public CommonResult insertPointsActInfo(@RequestBody MemberPointsActivityReq memberPointsActivityReq){

        MemberPointsActivity memberPointsActivity = BeanCopyUtil.copyProperties(memberPointsActivityReq, MemberPointsActivity.class);
        boolean save = memPointsActInfoService.save(memberPointsActivity);
        return save?CommonResult.success("保存成功"):CommonResult.failure("保存失败");
    }

    @ApiOperation("刷新缓存:积分活动基础信息")
    @PostMapping("/refreshCache")
    public CommonResult refreshCache(){
        memPointsActInfoService.refreshCache();
        return CommonResult.success("刷新缓存成功");
    }
}
